/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package woowcrawler;

import java.util.ArrayList;
import woowcrawler.html.*;
import java.io.BufferedReader;
import java.io.IOException;
import java.io.InputStreamReader;
import java.util.logging.Level;
import java.util.logging.Logger;
import java.net.*;
import java.util.regex.Matcher;
import java.util.regex.Pattern;

/**
 *
 * @author Heitor
 */
public class GerarURLs
{

    private static String base1url = "http://portal.acm.org/results.cfm?query=";
    private static String query = "";
    private static String base2url = "&querydisp=&source_query=&start=";
    private static String count = "";
    private static String base3url = "&srt=score%20dsc&short=0&source_disp=&since_month=&since_year=&before_month=&before_year=&coll=DL&dl=GUIDE&termshow=matchall&range_query=&CFID=11932354&CFTOKEN=15705811";
    private static String cfid = "";
    private static String base4url = "&CFTOKEN=";
    private static String cftoken = "";
    private static String urlprincipal = "http://portal.acm.org/";
    private static String complementoArtigo = "&preflayout=flat";

    //feito pelo Heitor

    private static String lerPortalACM()
    {
        String conteudo = "";
        try
        {
            URL u = new URL("http://portal.acm.org/");
            try
            {
                BufferedReader br = new BufferedReader(new InputStreamReader(u.openStream()));
                conteudo = br.readLine();
                String controlador = conteudo;

                while (controlador != null)
                {
                    String temp = br.readLine();
                    conteudo += temp + "\n";
                    controlador = temp;
                }
            }
            catch (IOException ex)
            {
                Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            }
        }
        catch (MalformedURLException ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
        }

        return conteudo;
    }

    private static String getStringDoCFIDeToken()
    {
        String regex = "cfid.*\" ";
        Pattern p = Pattern.compile(regex);

        String source = GerarURLs.lerPortalACM();

        Matcher m = p.matcher(source);

        String resultado = "";

        while (m.find())
        {
            resultado = m.group(0);
            break;
        }

        return resultado;
    }

    private static String getCFIDandCFToken(String rough)
    {
        String resultado = "";

        String temp[] = rough.split("&amp;cftoken=");

        for (int i = 5; i < temp[0].length(); i++)
        {
            resultado += temp[0].charAt(i);
        }

        resultado += "-" + temp[1].substring(0, temp[1].length() - 2);

        return resultado;
    }

    public static String[] gerarURLs(String temp, int quant)
    {
        String[] urls = new String[quant];

        for (int l = 0; l < temp.length(); l++)
        {
            if (temp.charAt(l) == ' ')
            {
                query += "%20";
            }
            else
            {
                query += temp.charAt(l);
            }
        }
        String[] cfidetoken = GerarURLs.getCFIDandCFToken(getStringDoCFIDeToken()).split("-");

        cfid = cfidetoken[0];
        cftoken = cfidetoken[1];

        for (int i = 0; i < quant; i++)
        {
            if (i == 0)
            {
                count = "1";
            }
            else
            {
                count = i * 2 + "1";
            }

            urls[i] = mergeURL();
        }

        return urls;
    }

    private static void reset()
    {
        count = query = cfid = cftoken = "";
    }

    private static String mergeURL()
    {
        return base1url + query + base2url + count + base3url + cfid + base4url + cftoken;
    }


    //Feito por Naara, Luane, Jorge

    public static ArrayList<String> PegarLinksArtigos(String link)
    {
        try
        {

            String[] urls = gerarURLs(link, 1);
            WoowDocument doc;
            String site;
            ArrayList<String> artigos = new ArrayList<String>();
            Attribute[] tags;
            for (int i = 0; i < urls.length; i++)
            {
                site = LerSite(urls[i]);
                doc = new WoowDocument(site);
                Tag[] elementos = doc.executeQuery(".medium-text");
                for (Tag tag : elementos)
                {
                    tags = tag.getAttributes();
                    artigos.add(urlprincipal + tags[0].getValue() + complementoArtigo);
                }
            }
            return artigos;

        }
        catch (Exception e)
        {
            return null;
        }



    }
    public static String LerSite(String link)
    {
        try {
            URL url = new URL(link);
            BufferedReader br = new BufferedReader(new InputStreamReader(url.openStream()));
            String documento = "";
            String aux = "";
            while ((aux = br.readLine()) != null) {
                documento += aux;
            }
            //o documento HTML não é valido, iPhode
            Matcher m = Pattern.compile("<html[^<>]*>").matcher(documento);
            if (!m.find()) {
                documento = "<" + documento.substring(documento.indexOf("html"));
            }
            return documento;
        } catch (Exception ex)
        {
            Logger.getLogger(Main.class.getName()).log(Level.SEVERE, null, ex);
            return null;
        }

    }
}
